package com.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemservice.mapper.RoleMapper;
import generator.domain.Premission;
import com.systemservice.service.PremissionService;
import com.systemservice.mapper.PremissionMapper;
import generator.domain.Role;
import generator.support.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author 杨健康
* @description 针对表【t_premission】的数据库操作Service实现
* @createDate 2022-06-20 10:46:52
*/
@Service
public class PremissionServiceImpl extends ServiceImpl<PremissionMapper, Premission>
    implements PremissionService{

    @Autowired
    PremissionMapper premissionMapper;

    @Autowired
    RoleMapper roleMapper;

    /**
     * 获取所有权限信息
     */
    @Override
    public List<Map<String, Object>> getPre() {
        QueryWrapper<Premission> preQw = new QueryWrapper<>();
        List<Premission> premissions = premissionMapper.selectList(preQw);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Premission premission : premissions) {
            Map<String, Object> map = new HashMap<>();
            map.put("premission", premission);
            QueryWrapper<Role> roleQw = new QueryWrapper<>();
            roleQw.eq("role_id", premission.getPremissionRoleId());
            List<Role> roles = roleMapper.selectList(roleQw);
            if (roles.isEmpty()){
                Role r = new Role();
                r.setRoleName("该角色已被删除");
                map.put("role", r);
            } else {
                map.put("role", roles.get(0));
            }
            result.add(map);
        }
        return result;
    }

    /**
     * 模糊查询权限信息
     */
    @Override
    public List<Map<String, Object>> queryPre(String role, String pre) {
        int roleId = -1;
        if (!"".equals(role)){
            // 查询该角色是否存在
            QueryWrapper<Role> roleQw = new QueryWrapper<>();
            roleQw.eq("role_name", role);
            List<Role> roles = roleMapper.selectList(roleQw);
            if (roles.isEmpty()){
                return null;
            }
            roleId = roles.get(0).getRoleId();
        }

        // 查询权限信息是否存在
        String premission = "-1";
        if (!"".equals(pre)){
            premission = pre;
        }
        QueryWrapper<Premission> preQw = new QueryWrapper<>();
        if (roleId != -1){
            preQw.eq("premission_role_id", roleId);
        }
        if (!"-1".equals(premission)){
            preQw.eq("premission_no", premission);
        }

        List<Premission> premissions = premissionMapper.selectList(preQw);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Premission premission1 : premissions) {
            Map<String, Object> map = new HashMap<>();
            map.put("premission", premission1);
            QueryWrapper<Role> roleQw = new QueryWrapper<>();
            roleQw.eq("role_id", premission1.getPremissionRoleId());
            List<Role> roles = roleMapper.selectList(roleQw);
            if (roles.isEmpty()){
                Role r = new Role();
                r.setRoleName("该角色已被删除");
                map.put("role", r);
            } else {
                map.put("role", roles.get(0));
            }
            result.add(map);
        }
        return result;
    }

    /**
     * 添加权限信息
     */
    @Override
    public ResponseResult<Object> addPre(Premission pre) {
        // 查看权限信息是否合法
        String[] premissionNo = new String[]{"员工管理", "请假管理", "报销管理", "部门管理", "密码重置"};
        boolean judge = false;
        for (String s : premissionNo) {
            if (pre.getPremissionNo().equals(s)){
                judge = true;
                break;
            }
        }
        if (!judge){
            return new ResponseResult<>(400, "权限信息有误，请重新选择");
        }

        // 查看角色信息是否合法
        QueryWrapper<Role> roleQw = new QueryWrapper<>();
        roleQw.eq("role_id", pre.getPremissionRoleId());
        List<Role> roles = roleMapper.selectList(roleQw);
        if (roles.isEmpty()){
            return new ResponseResult<>(400, "角色信息有误，请重新选择");
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        pre.setPremissionCreateTime(format.format(new Date()));
        if (premissionMapper.insert(pre) > 0){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "信息已存在");
    }

    @Override
    public ResponseResult<Object> editPre(Premission pre) {
        // 查看权限信息是否合法
        String[] premissionNo = new String[]{"员工管理", "请假管理", "报销管理", "部门管理", "密码重置"};
        boolean judge = false;
        for (String s : premissionNo) {
            if (pre.getPremissionNo().equals(s)){
                judge = true;
                break;
            }
        }
        if (!judge){
            return new ResponseResult<>(400, "权限信息有误，请重新选择");
        }

        // 查看角色信息是否合法
        QueryWrapper<Role> roleQw = new QueryWrapper<>();
        roleQw.eq("role_id", pre.getPremissionRoleId());
        List<Role> roles = roleMapper.selectList(roleQw);
        if (roles.isEmpty()){
            return new ResponseResult<>(400, "角色信息有误，请重新选择");
        }

        if (premissionMapper.updateById(pre) > 0){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败");
    }
}




